import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export function createStore() {
    return new Vuex.Store({
        state: {
            articles: []
        },

        mutations: {
            setArticles(state, data) {
                state.articles = data
            }
        },
        actions: {
            // 这里需要返回一个Promise
            async fetchArticles({
                commit
            }) {
                const {
                    data
                } = await axios.get("https://conduit.productionready.io/api/articles")
                commit('setArticles', data.articles)

            }
        },
    })
}